package com.diyizu.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diyizu.youdianda.common.PageResult;
import com.diyizu.youdianda.common.R;
import com.diyizu.youdianda.dto.YddAdvimgDto;
import com.diyizu.youdianda.entity.YddAdvimg;
import com.diyizu.youdianda.entity.YddAdvpos;
import com.diyizu.youdianda.service.YddAdvimgService;
import com.diyizu.youdianda.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {

    @Autowired
    private YddAdvimgService yddAdvimgService;
    @Autowired
    private YddAdvposService yddAdvposService;

    /**
     * 根据id查询广告图详情
     * @param id
     * @return
     */
    @RequestMapping("/rowInfo")
    public R<YddAdvimg> getById(Integer id){
        log.info("前端传过来的数据:",id);
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        return R.success(yddAdvimg);
    }

    @GetMapping("/list")
    public R page(String advimgdesc,String advimgpos,Integer page,Integer limit){
        log.info("advimgdesc = {},advimgpos = {},page = {},limit = {}",advimgdesc,advimgpos,page,limit);

        //构造分页构造器
        Page<YddAdvimg> pageInfo = new Page<>(page,limit);

        //构造条件构造器
        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        //添加条件
        wrapper.like(StringUtils.isNotEmpty(advimgdesc),YddAdvimg::getAdvimgdesc,advimgdesc);
        wrapper.like(StringUtils.isNotEmpty(advimgpos),YddAdvimg::getAdvimgpos,advimgpos);

        //执行查询命令
        yddAdvimgService.page(pageInfo,wrapper);
        //封装结果集
        PageResult pageResult =new PageResult();

        pageResult.setCurrentPage(pageInfo.getCurrent());
        pageResult.setCount(pageInfo.getTotal());
        pageResult.setPageSize(pageInfo.getSize());
        pageResult.setTotalPages(pageInfo.getPages());



        List<YddAdvimg> records = pageInfo.getRecords();
        List<YddAdvimgDto> list = records.stream().map((item)->{

            YddAdvimgDto advimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(item,advimgDto);
            LambdaQueryWrapper<YddAdvpos> lambdaQueryWrapperwrapper =new LambdaQueryWrapper<>();
            lambdaQueryWrapperwrapper.eq(item.getAdvimgpos()!=null,YddAdvpos::getId,item.getAdvimgpos());
            List<YddAdvpos> yddAdvposs = yddAdvposService.list();
            String advimgpos1 = item.getAdvimgpos();
            int advimgpos2 = Integer.parseInt(advimgpos1);
            for (int i = 0; i < yddAdvposs.size(); i++) {
                Integer id = yddAdvposs.get(i).getId();
                if(advimgpos2 == id){
                    advimgDto.setAdvposname(yddAdvposs.get(i).getAdvposname());
                }
            }
            return advimgDto;
        }).collect(Collectors.toList());
        pageResult.setData(list);
        return R.success(pageResult);
    }

    /**
     * 根据id修改广告图位信息
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvimg yddAdvimg){
        log.info("前端传过来的数据:",yddAdvimg);
        yddAdvimgService.updateById(yddAdvimg);
        return R.success("广告图信息修改成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> deleteById(Integer id){
        log.info("前端页面传过来的信息:",id);
        //判断DefaultData是否为默认值，如果为默认值则不能删除
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        if(yddAdvimg.getDefaultData()!=null&&yddAdvimg.getDefaultData().equals("1")){
            return R.error("默认数据,不能删除");
        }
        yddAdvimgService.removeById(id);
        return R.success("信息删除成功");
    }

    /**
     * 添加广告图
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddAdvimg yddAdvimg){
        log.info("前端传过来的信息:",yddAdvimg);
        yddAdvimgService.save(yddAdvimg);
        return R.success("信息添加成功");
    }
}
